package com.example.mysharding.utils;

import com.alibaba.excel.util.DateUtils;
import com.example.mysharding.bean.IdWraper;

import java.util.Calendar;
import java.util.Date;

public class ShardingUtils {


    public static final String DATE_FORMAT_FOR_DAY = "yyyyMMdd HH:mm:ss";

    public static String TEN_ZERO_STR = "0000000000";

    /**
     * 获取非自增分布式唯一id
     * 前8位为精确到天的显式时间,后10位为ip地址+当前时间+uuid的正数hashcod
     * @return
     */
    public static Long genBussinessId() {
        Calendar calendar = Calendar.getInstance();
        Date currentTime = calendar.getTime();
        IdWraper idWraper = new IdWraper(currentTime);
        Long currentDay4Long = Long.parseLong(DateUtils.format(currentTime, DATE_FORMAT_FOR_DAY).
                substring(0, 8) + TEN_ZERO_STR);
        return currentDay4Long + idWraper.hashCode();
    }
}
